package com.example.behaviour.IM;

import com.example.behaviour.handler.*;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.util.CharsetUtil;

/**
 * @program: qingcheng
 * @author: XIONG CHUAN
 * @create: 2019-04-28 15:10
 * @description: 服务端初始化，客户端与服务器端连接一旦创建，这个类中方法就会被回调，设置出站编码器和入站解码器
 **/

public class NettyServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel channel) throws Exception {

        channel.pipeline().addLast(new HttpServerCodec())
                .addLast(new LoggingHandler())
                .addLast("decoder",new StringDecoder(CharsetUtil.UTF_8))
                .addLast("encoder",new StringEncoder(CharsetUtil.UTF_8))
                .addLast(new ChunkedWriteHandler())
                .addLast(new HttpObjectAggregator(1024*64))
                .addLast(new WebSocketServerProtocolHandler("/websocket"))
                //.addLast(new WebSocketHandler());

                .addLast(new OnlineMessageHandler())
//                .addLast(new AudioRequestHandler())

                .addLast(new AddFriendRequestMessageHandler())
//                .addLast(new LoginRequestMessageHandler())
                .addLast(new ChatRequestMessageHandler())
                .addLast(new AckRequestMessageHandler());
//                .addLast(new QuitHandler());
//                .addLast(new NettyServerHandler());
    }
}
